﻿using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GanagroLiteData;
using Csla.Validation;

namespace GanagroLiteBO
{
    public class OpArgs : IEditableObject, INotifyPropertyChanged, IDataErrorInfo
    {
        private TiposOperacion _tipoOperacion;
        private bool m_Editing;

        #region constructor
        public OpArgs(TiposOperacion tOp)
        {
            _tipoOperacion = tOp;
            AddBusinessRules();
        }
        #endregion

        #region Properties

        string _finca;
        public string Finca
        {
            get { return _finca; }
            set { _finca = value; }
        }

        byte _lote;
        public byte Lote
        {
            get { return _lote; }
            set
            {
                if (_lote != value)
                {
                    _lote = value;
                    FirePropertyChangedNotification("Lote");
                }
            }
        }

        byte _estado;
        public byte Estado
        {
            get { return _estado; }
            set
            {
                if (_estado != value)
                {
                    _estado = value;
                    FirePropertyChangedNotification("Estado");
                }
            }
        }


        string _sexo;
        public string Sexo
        {
            get { return _sexo; }
            set
            {
                if (_sexo != value)
                {
                    _sexo = value;
                    FirePropertyChangedNotification("Sexo");
                }
            }
        }

        string _marca;
        public string Marca
        {
            get { return _marca; }
            set
            {
                if (_marca != value)
                {
                    _marca = value;
                    FirePropertyChangedNotification("Marca");
                }
            }
        }


        bool _alojo;
        public bool Alojo
        {
            get { return _alojo; }
            set
            {
                if (_alojo != value)
                {
                    _alojo = value;
                    FirePropertyChangedNotification("Alojo");
                }
            }
        }

        Int32 _valorKilo;
        public Int32 ValorKilo
        {
            get { return _valorKilo; }
            set
            {
                if (_valorKilo != value)
                {
                    _valorKilo = value;
                    FirePropertyChangedNotification("ValorKilo");
                }
            }
        }

        Int32 _edad;
        public Int32 Edad
        {
            get { return _edad; }
            set
            {
                if (_edad != value)
                {
                    _edad = value;
                    FirePropertyChangedNotification("Edad");
                }
            }
        }

        #endregion Properties


        public TiposOperacion TipoOperacion
        {
            get { return _tipoOperacion; }
            set { _tipoOperacion = value; }
        }

        #region Validaciones
        private ValidationRules _validationRules;
        public ValidationRules ValidationRules
        {
            get
            {
                if (_validationRules == null)
                    _validationRules = new ValidationRules(this);
                return _validationRules;
            }
        }

        public bool Isvalid()
        {
            return ValidationRules.IsValid;
        }


        #endregion

        private void AddBusinessRules()
        {
            try
            {
            }
            catch (Exception ex)
            {
                throw (new Exception("AddBusinessRules: " + ex.Message, ex));
            }

        }

        #region INotifyPropertyChanged Implementation
        protected void FirePropertyChangedNotification(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
            ValidationRules.CheckRules(propName);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region IDataErrorInfo Members

        //				    private string m_Error = string.Empty;
        //				    private Hashtable m_PropErrors = new Hashtable();

        [Browsable(false)]
        public string Error
        {
            get
            {
                if (!ValidationRules.IsValid)
                {  return ValidationRules.GetBrokenRules().ToString(Csla.Validation.RuleSeverity.Error);}
                else
                { return String.Empty; }
           }
        }

        [Browsable(false)]
        public string this[string columnName]
        {
            get
            {
                string result = string.Empty;
                if (!ValidationRules.IsValid)
                {
                    BrokenRule rule = ValidationRules.GetBrokenRules().GetFirstBrokenRule(columnName);
                    if (rule != null)
                        result = rule.Description;
                }

                return result;
            }
        }
        #endregion

        #region IEditableObject Members

        public void BeginEdit()
        {
            //if (!m_Editing)
            //{
            //}

            m_Editing = true;
        }

        public void CancelEdit()
        {
            m_Editing = false;
        }

        public void EndEdit()
        {
            //if (m_Editing)
            //{
            //}

            m_Editing = false;
        }

        #endregion

    }
}
